#!/usr/bin/awk -f 

# interface server access log hashfile
# 按pid、小时切分日志，注意系统ulimit 限制

# \/layout\/phone phone 接口
# \/openapi-cms\/ipad ipad首页
# (openapi-wireless)?\/(get|add|send|search|login|verifyUser|doRegister|hellotest|test) api 2.0 接口

# awk -f acccess_log.awk -v mixpid="a8f2373285115c07|87c959fb273378eb" -v fid="xxxxx" -v output="/xxx/" -v fname="access_00" -v nomatch=".nomatch" xxx.log

# mixpid    为client version > 2.0 && < api 2.0 接口 可选
# fid    为过滤pid 可选
# output    文件散列路径,例 2011/12/14
#] fname     散列文件名
# nomatch   未匹配日志后缀名，可选

BEGIN{
    if(!output || !fname) {
        print "usage: awk -f acccess_log.awk -v output=value -v fname=value [-v mixpid=value -v fid=value | -v nomatch=value] file ...";
        exit;
    }else if(system("test -d " output) == 1) {
        print "Folder: "output " : is no exists!";
        exit;
    }
    PID_length = 16;

    # MIX pid api 2.0/3.0 混合调用
    PID_Match = mixpid ? mixpid : "a8f2373285115c07|87c959fb273378eb";

    # filter pid 
    PID_filter = fid ? fid : "127f7a5e3a366bd0|a8c022bcd2adcbee";

    Time_begin = systime();

    print fname;
    print strftime("%Y-%m-%d %T",Time_begin) " Starting...";
}

{
    # 替换Requset url, GET POST param , User-Agent 中空格
    if(NF != 10){
        Str="";
        # 以 " 切分数组长度为10，替换其中的空格为%20
        split($0,Arr,/\042/);
        Arr_len = length(Arr);
        if(Arr_len != 10){
            for(i=1;i<=Arr_len;i++){
                if(i == 2){
                    Arr[i] = "\""Arr[i]"\"";
                }else if(i == 4 || i == 6 || i == 8 || i == 10){
                    gsub(/ /,"%20",Arr[i]);
                    Arr[i] = "\""Arr[i]"\"";
                }
                Str = Str""Arr[i];
            }
            $0 = Str;
        }else{
            Outfile = output"/error";
            print $0 >> Outfile;
            next;
        }
    }

    # GET POST param 
    Param = $5$6;

    # (APhone version < 2.0 && mixpid !~ PID_Match) || (/layout/mtk | /layout/apple/ | /openapi-cms/android/ | /internal/ | /ctrl/) 
    if(Param ~ PID_filter || ($4 ~ /^\"(\/openapi-wireless)?\/(get|add|send|search|login|verifyUser|doRegister|hellotest|test)/ && Param !~ PID_Match) || ($4 ~ /^\"(\/layout\/mtk|\/layout\/apple|\/openapi-cms\/android|\/internal|\/ctrl)/)){
        if(nomatch){
            hash_file("nomatch");
        }
    }else{
        hash_file("match");
    }
}

END{
    if(Time_begin){
        Time_end = systime();
        print strftime("%Y-%m-%d %T",Time_end) " End";
        print "Take Time: " Time_end - Time_begin "s";
        print "Row Total: " NR "\n";
    }
}

#hash_file
function hash_file(type) {
    #get pid
    PID_offset = index(Param,"pid=");
    PID = (PID_offset == 0) ? "_nopid" : substr(Param,PID_offset+4,PID_length);
    if(PID != "_nopid" && PID !~ /^[0-9a-z]{16}$/){
        PID = "_nopid";
    }

    Outfile = (type == "match") ? output"/"PID"/"fname : output"/"PID"/"fname""nomatch;

    # 需修改ulimit 
    if(PID_ARR[PID]){
        print $0 >> Outfile;
    }else{
        PID_ARR[PID] = "1";
        system("mkdir -p " output"/"PID);
        print $0 >> Outfile;
    }
}
